import random
import string
from common.operation_excel import data
from common.operation_excel import OperationExcel

file_path = '../data/test_table.xlsx'
operation = OperationExcel(file_path)


class RegisterData(object):

    def get_random_tel( self ):
        """随机生成电话"""
        mobile_begin_seed = ['134', '135', '136', '137', '138', '139', '150', '151', '152', '157', '158', '159', '182',
                             '183',
                             '187', '188', '130', '131', '132', '155', '156', '175', '185', '186', '133', '153', '177',
                             '180',
                             '181', '189']
        mobile_seed = string.digits
        self.tel = random.choice(mobile_begin_seed) + ''.join(random.choice(mobile_seed) for m in range(8))
        return self.tel

    def get_random_password( self ):
        """随机生成密码6-16位"""
        password_seed = string.digits + string.ascii_letters + string.punctuation
        self.password = ''.join(random.choice(password_seed) for p in range(random.randint(6, 16)))
        return self.password

    def get_repassword( self, password ):
        """获得确认密码和密码相等"""

        self.repassword = password
        return self.repassword

    def get_rand_username( self ):
        """随机生成用户名"""
        first_name = ['赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋', '沈', '韩', '杨', '朱', '秦', '尤',
                      '许',
                      '何', '吕', '施', '张', '孔', '曹', '严', '华', '金', '魏', '陶', '姜', '戚', '谢', '邹', '喻', '柏', '水', '窦',
                      '章',
                      '云', '苏', '潘', '葛', '奚', '范', '彭', '郎', '鲁', '韦', '昌', '马', '苗', '凤', '花', '方', '俞', '任', '袁',
                      '柳',
                      '酆', '鲍', '史', '唐', '费', '廉', '岑', '薛', '雷', '贺', '倪', '汤', '滕', '殷', '罗', '毕', '郝', '邬', '安',
                      '常',
                      '乐', '于', '时', '傅', '皮', '卞', '齐', '康', '伍', '余', '元', '卜', '顾', '孟', '平', '黄', '和', '穆', '萧',
                      '尹',
                      '姚', '邵', '堪', '汪', '祁', '毛', '禹', '狄', '米', '贝', '明', '臧', '计', '伏', '成', '戴', '谈', '宋', '茅',
                      '庞',
                      '熊', '纪', '舒', '屈', '项', '祝', '董', '梁']

        lasts_name = ['兮陌', '颜汐', '诺菲', '蝶恋', '浮华', '繁花', '秋雨', '落霜', '轨忆', '末白', '梦徒', '往非', '朽命', '弃怀', '妆解',
                      '念她', '拾心', '如梦', '南城', '浮伤', '淡忆', '束心', '填谎', '默忆', '逆缘', '四忌', '望晴', '年轮', '若风', '凉生', '空岛',
                      '半夏', '疏离', '私念', '负约', '旧颜', '沉世', '素颜', '静谧', '薄姬', '忆白', '留井', '蔷薇', '忧深', '风掠', '萧兮', '独醉',
                      '黎铁', '擒拿', '静候', '一线', '差池', '借口', '没有', '方向', '梨落', '瑾色', '呢喃', '琴瑟', '残舞', '離殇', '陌颜', '囬首',
                      '肆意', '弄裳', '暗花', '雁芙', '灵瞳', '从筠', '细纱', '风惠']
        self.username = random.choice(first_name) + random.choice(lasts_name)
        return self.username

    def RandomEmail( self, emailType=None, rang=None ):
        """随机生成邮箱"""
        emailtype = ["@qq.com", "@163.com", "@126.com"]
        # 如果没有指定邮箱类型，默认在 __emailtype中随机一个
        if emailType == None:
            randomEmail = random.choice(emailtype)
        else:
            randomEmail = emailType
        # 如果没有指定邮箱长度，默认在4-12之间随机
        if rang == None:
            rang = random.randint(4, 12)
        else:
            rang = int(rang)
        Number = "0123456789qbcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPWRSTUVWXYZ"
        randomNumber = "".join(random.choice(Number) for i in range(rang))
        self.email = randomNumber + randomEmail
        return self.email

    def get_question( self, ):
        """随机选择密码问题"""
        self.question = random.randint(1, 9)
        return self.question

    def get_answer( self, ):
        """随机生成答案"""
        answer_list = ['硝烟漫说己随风', '战线何曾旯旮空', '纵使孤身守高地', '不成仁处也英雄',
                       '炎炎盛夏荔枝娇', '喜果丰收四海飘', '昔望唐时妃子宠', '而今百姓尽逍遥',
                       '庭前栀子自天来', '暗把幽香玉瓣裁', '最是凉丛芳榭处', '依依顾盼忍离开',
                       '流水浮云者去多', '于今剩得有如何', '每每中宵清自守', '只同明月一厮磨',
                       '每忆流年总感伤', '同窗情谊最难忘', '万千往事随风去', '执手唯嗟鬓已霜',
                       '栀子花和通明大诗兄', '浓香满院任风来', '绿叶琼花一色裁',
                       '倩影婷婷犹带笑', '千枝万朵向君开'
                       ]
        self.answer = random.choice(answer_list)
        return self.answer

    def get_address( self, ):
        """随机生成答案"""
        address_list = ['武侯区','青羊区','高新区','金融城','金牛区','郫都区','龙泉驿','武侯祠'
                       ]
        self.address = random.choice(address_list)
        return self.address





    def write_info_question( self, ):
        """选择密码问题"""
        self.data_info = {"username": self.username,
                          "email": self.email,
                          "password": self.password,
                          "repassword": self.repassword,
                          "tel": self.tel,
                          'question': self.question,
                          'answer': self.answer}
        data.append(self.data_info)

    def write_info( self, question=0, answer=None ):
        """不选择密码问题"""
        self.data_info = {"username": self.username,
                          "email": self.email,
                          "password": self.password,
                          "repassword": self.repassword,
                          "tel": self.tel,
                          'question': question,
                          'answer': answer}
        data.append(self.data_info)

    def all_data(self):
        """注册数据全部填写"""
        data = {}
        data["username"] = self.get_rand_username()
        data["email"] = self.RandomEmail()
        data["password"] = self.get_random_password()
        data["cf_password"] = data["password"]
        data["tel"] = self.get_random_tel()
        data["answer"] = self.get_answer()
        return data

    def data_values_for_list(self,data):
        """
        字典值转列表
        :param data: 是一个字典
        :return: 列表
        """
        values = []
        for key in data.keys():
            values.append(data[key])
        return values




    def check_email( self, email ):
        for user in data:
            if email.strip() == user["email"]:  # 判断邮箱是否已存在
                print("邮箱已被使用!请从新输入")
                return False
        return email

    def check_username( self, username ):
        """
        验证用户名,不能重复
        :param username:
        :return:
        """
        for user in data:
            if username.strip() == user["username"]:  # 判断用户名是否已存在
                print("用户名已存在!请重新输入")
                return False
        else:
            return username
